package com.shj.em.biz.prod.controller;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.prod.entity.OrderOutdate;
import com.shj.em.biz.prod.service.OrderOutdateService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 出货日期调整信息 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-12
 */
@Controller
@Slf4j
@RequestMapping("/prod/orderOutdate")
public class OrderOutdateController extends BaseController<OrderOutdate>{

    private String prefix = "prod/outdate";

    @Autowired
    OrderOutdateService orderoutdateService;

    @GetMapping("/gotoSearch")
    public String gotoSearch(){
        return prefix + "/search";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageVO<OrderOutdate> list(@RequestBody Map<String, Object> params){
        Page<OrderOutdate> page = orderoutdateService.queryOrderOutdatePage(params);
        PageVO<OrderOutdate> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/gotoAdd")
	String gotoAdd(Model model){
        model.addAttribute("isEdit", false);
        model.addAttribute("orderoutdate", new OrderOutdate());
	    return prefix + "/add";
	}

	@GetMapping("/gotoEdit/{id}")
	String gotoEdit(@PathVariable("id") Long id, Model model){
        OrderOutdate orderoutdate = orderoutdateService.selectById(id);
		model.addAttribute("orderoutdate", orderoutdate);
        model.addAttribute("isEdit", true);
	    return prefix + "/add";
	}

	@Log("出货日期调整")
	@ResponseBody
	@PostMapping("/add")
	public ResMsg add(OrderOutdate orderoutdate){
        try{
            orderoutdateService.insert(orderoutdate);
            return ResMsg.ok(new OrderOutdate());
        }catch(Exception e){
            log.error("创建出货日期调整信息失败", e);
            return ResMsg.error("创建出货日期调整信息失败");
        }

	}

    @Log("更新出货日期")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(OrderOutdate orderoutdate){
        try{
            orderoutdateService.updateById(orderoutdate);
            return ResMsg.ok();
        }catch(Exception e){
            log.error("更新出货日期调整信息失败", e);
            return ResMsg.error("更新出货日期调整信息失败");
        }

	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            orderoutdateService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        orderoutdateService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}

}

